package com.example.hm_dtfkz.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dq.ad.UMManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.databinding.DialogSwitchAnswerBinding;
import com.example.hm_dtfkz.litepal.AnswerLitePalEntity;
import com.example.hm_dtfkz.ui.adapter.SwitchAnswerAdapter;
import com.example.hm_dtfkz.util.ActivityClickUtil;
import com.example.hm_dtfkz.util.DisplayUtil;

import org.jetbrains.annotations.NotNull;
import org.litepal.LitePal;

/**
 * FileName: DialogSwitchAnswer
 * Author: admin
 * Date: 2022/1/19 16:22
 * Description:
 */
public class DialogSwitchAnswer extends Dialog implements OnItemClickListener, View.OnClickListener {

    private DialogSwitchAnswerBinding switchAnswerBinding;
    private SwitchAnswerAdapter answerAdapter;
    private OnClickLister clickLister;
    private final Activity activity;

    public DialogSwitchAnswer(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switchAnswerBinding = DialogSwitchAnswerBinding.inflate(getLayoutInflater());
        setContentView(switchAnswerBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
        initLister();
    }

    private void initView() {
        UMManager.onEvent(UMManager.Event.classification_window);
        answerAdapter = new SwitchAnswerAdapter();
        switchAnswerBinding.recyclerView.setAdapter(answerAdapter);
        answerAdapter.setList(LitePal.findAll(AnswerLitePalEntity.class));
    }

    public DialogSwitchAnswer setList() {
        answerAdapter.setList(LitePal.findAll(AnswerLitePalEntity.class));
        return this;
    }

    private void initLister() {
        answerAdapter.setOnItemClickListener(this);
        switchAnswerBinding.imgClose.setOnClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (this.clickLister != null) {
            this.clickLister.unlock(this, answerAdapter.getData().get(position));
        }
    }

    public DialogSwitchAnswer setOnClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (v.getId() == R.id.imgClose) {
            dismiss();
        }
    }

    public interface OnClickLister {
        void unlock(Dialog dialog, AnswerLitePalEntity litePalEntity);
    }

}